<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/7/10
 * Time: 10:16
 */

namespace app\controller\sys;

use app\model\AttrKey;
use app\model\AttrValue;
use app\model\Goods AS model;
use app\controller\Base;
use app\model\Attr;
use app\model\InfoContent;

class Goods extends Base
{
    public function list(){

    }

    public function info(){

    }

    public function status($id,$status){
        model::where('id',$id)->save(['status' => $status]);
        return $this->msg();
    }

    public function save($id = 0){
        $post = input('post.');
        if (empty($post['name']))
            return $this->fail('商品名称必须上传');
        if (empty($post['price']))
            return $this->fail('商品价格必须上传');
        if (!empty($post['images'])){
            foreach ($post['images'] AS $key => $value)
                \app\model\Image::where('id',$value['id'])->save($value);
            $images_id = json_encode(array_column($post['images'],'id'));
        }
        $data = ['name' => $post['name'],'image_id' => $post['image_id'], 'price' => $post['price'], 'images_id' => $images_id ?? '[]','info' => $post['info'], 'nav_id' => $post['nav_id'] ?? ''];
        if (!empty($id)){
            $goods = model::find($id);
            $goods->save($data);
        }else{
            $goods = model::create($data);
        }
        if (!empty($post['content'])){
            $find = InfoContent::where('goods_id',$id)->find();
            if (empty($find->id))
                InfoContent::create([
                    'content' => $post['content'],
                    'goods_id' => $goods->id
                ]);
            else
                $find->save(['content' => $post['content']]);
        }
        if (!empty($post['attr'])){
            Attr::where('goods_id',$goods->id)->delete();
            foreach ($post['attr'] AS $key => $value)
                Attr::create([
                    'name' => $value['name'],
                    'price' => $value['price'],
                    'info' => $value['info'],
                    'stock' => $value['stock'],
                    'image_id' => $value['image_id'],
                    'goods_id' => $goods->id,
                ]);
        }
        if (!empty($post['attrkey'])){
            AttrKey::where('goods_id',$goods->id)->delete();
            AttrValue::where('goods_id',$goods->id)->delete();
            foreach ($post['attrkey'] AS $key => $value){
                $keys = AttrKey::create([
                    'name' => $value['key'],
                    'goods_id' => $goods->id
                ]);
                foreach ($value['value'] AS $index => $item)
                    AttrValue::create([
                        'name' => $item,
                        'image_id' => $value['image'][$index] ?? 0,
                        'attr_key_id' => $keys->id,
                        'goods_id' => $goods->id
                    ]);
            }
        }
        return $this->msg();
    }

    public function delete($id){
        model::find($id)->delete();
        return $this->msg();
    }
}